package com.tfswx.ocrengine;

import com.tfswx.ocrengine.dto.OcrOutputDTO;
import com.tfswx.ocrengine.dto.OcrResultGetInputDTO;
import com.tfswx.ocrengine.dto.WjOcrSbjgGetOutputDTO;
import com.tfswx.ocrengine.enums.YqlxEnum;
import com.tfswx.ocrengine.executor.AbstractOcrExecutor;
import com.tfswx.ocrengine.util.Base64Util;
import com.tfswx.ocrengine.util.PdfBase64StrTool;
import com.tfswx.ocrengine.util.SbjgUtils;

import java.io.IOException;
import java.util.ArrayList;

/**
 * @author Ricky
 * @date 2021/5/27
 */
public class OcrEngineUtil {
    private OcrEngineUtil() {
    }

    public static WjOcrSbjgGetOutputDTO execute(YqlxEnum yqlx, String host, OcrResultGetInputDTO ocrResultGetInputDTO) throws AbstractOcrExecutor.EngineException, IOException {
        AbstractOcrExecutor instance = AbstractOcrExecutor.getInstance(yqlx);
        instance.setHost(host);
        String wjnr = ocrResultGetInputDTO.getWjnr();
        OcrOutputDTO ocrOutputDTO = instance.execute(wjnr);
        ArrayList<OcrOutputDTO> ocrOutputDtoList = new ArrayList<>();
        ocrOutputDtoList.add(ocrOutputDTO);

        boolean sfwjnr = ocrResultGetInputDTO.isSfwjnr();
        if (sfwjnr) {
            byte[] pdfBytes = PdfBase64StrTool.getPdfBytes(ocrOutputDTO, Base64Util.getByteArray(wjnr));
            ocrOutputDTO.setPdfStr(Base64Util.getBase64String(pdfBytes));
        }

        boolean sfwjwb = ocrResultGetInputDTO.isSfwjwb();
        boolean sfwjxx = ocrResultGetInputDTO.isSfwjxx();
        String wjbh = ocrResultGetInputDTO.getWjbh();
        String wjhz = ocrResultGetInputDTO.getWjhz();
        return SbjgUtils.packageResponseStr(wjbh, wjhz, ocrOutputDtoList, sfwjwb, sfwjxx, sfwjnr);
    }
}
